package com.wuneng.ircontrol.module.act;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.hardware.ConsumerIrManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import butterknife.BindView;
import butterknife.OnClick;

import com.wuneng.commonlibrary.util.JsonUtils;
import com.wuneng.ircontrol.R;
import com.wuneng.ircontrol.common.util.MyBaseAct;
import com.wuneng.ircontrol.myUtil.CodeCommand;
import com.wuneng.ircontrol.myUtil.LocaleHelper;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Locale;

public class HomeActivity extends MyBaseAct {
    @BindView(R.id.tvCopyRight)
    TextView tvCopyRight;
    @BindView(R.id.btnLanguage)
    Button btnLanguage;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.btnStop)
    Button btnStop;
    @BindView(R.id.btnOpen)
    Button btnOpen;
    @BindView(R.id.btnLocal)
    Button btnLocal;
    @BindView(R.id.btnExit)
    Button btnExit;
    @BindView(R.id.btnPlus)
    Button btnPlus;
    @BindView(R.id.btnClose)
    Button btnClose;
    @BindView(R.id.btnRemote)
    Button btnRemote;
    @BindView(R.id.btnEnter)
    Button btnEnter;
    @BindView(R.id.btnMin)

    Button btnMin;

    private ConsumerIrManager consumerIrManager;


    @Override
    protected void bindListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LocaleHelper.onCreate(this);
        super.onCreate(savedInstanceState);

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);//实现状态栏图标和文字颜色为暗色
    }

    @Override
    protected void initMyView() {


        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        tvCopyRight.setText("Copyright © " + year + getResources().getString(R.string.company) + " All Rights Reserved. ");


    }

    @Override
    protected void onResume() {
        super.onResume();
        updateButtonBackground();
    }

    private void updateButtonBackground() {
        String lang = LocaleHelper.getPersistedData(this, Locale.getDefault().getLanguage());
        if (lang.equals("en")) {
            btnLanguage.setBackgroundResource(R.mipmap.en_c);
        } else {
            btnLanguage.setBackgroundResource(R.mipmap.c_en);
        }
    }

    @Override
    protected void initMyData() {
        consumerIrManager = (ConsumerIrManager) this.getSystemService(CONSUMER_IR_SERVICE);
        if (!hasIrFunctionality()) {
            new AlertDialog.Builder(this).setMessage(R.string.tip_content).setTitle(R.string.tip).setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
//                    finish();
                }
            }).show();
        }
    }

    @Override
    protected int getMyContentViewLayoutID() {
        return R.layout.act_home;
    }


    @OnClick({R.id.btnStop, R.id.btnOpen, R.id.btnLocal, R.id.btnExit, R.id.btnPlus, R.id.btnClose, R.id.btnRemote, R.id.btnEnter, R.id.btnMin, R.id.btnLanguage})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnLanguage:
                String lang = LocaleHelper.getPersistedData(this, Locale.getDefault().getLanguage());
                if (lang.equals("en")) {
                    btnLanguage.setBackgroundResource(R.mipmap.c_en);
                    LocaleHelper.setLocale(this, "zh");
                } else {
                    LocaleHelper.setLocale(this, "en");
                    btnLanguage.setBackgroundResource(R.mipmap.en_c);
                }

                this.recreate(); // 重新创建Activity以应用新的语言设置
                break;
            case R.id.btnStop:
                send(CodeCommand.stop);
                break;
            case R.id.btnOpen:
                send(CodeCommand.open);
                break;
            case R.id.btnLocal:
                send(CodeCommand.local);
                break;
            case R.id.btnExit:
                send(CodeCommand.back);
                break;
            case R.id.btnPlus:
                send(CodeCommand.plus);
                break;
            case R.id.btnClose:
                send(CodeCommand.close);
                break;
            case R.id.btnRemote:
                send(CodeCommand.remote);
                break;
            case R.id.btnEnter:
                send(CodeCommand.confirm);
                break;
            case R.id.btnMin:
                send(CodeCommand.min);
                break;
        }
    }

    private void send(int[] data) {
        if (consumerIrManager != null && consumerIrManager.hasIrEmitter()) {
            consumerIrManager.transmit(38000, data);
        } else {
            showToast(getResources().getString(R.string.tip_content));
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public boolean hasIrFunctionality() {
        // 判断是否有红外发射器
        return consumerIrManager != null && consumerIrManager.hasIrEmitter();
    }


}
